package xian.woniuxy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xian.woniuxy.domain.ResultVo;
import xian.woniuxy.entity.Permission;
import xian.woniuxy.service.PermissionService;

import java.util.List;

/**
 * @author gao
 * @date 2023/07/03 11:31:58
 */
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @PostMapping("permission/save")
    public ResultVo save(@RequestBody Permission permission) {
        int rows = PermissionController.this.permissionService.save(permission);
        return rows > 0 ? ResultVo.success(permission) : ResultVo.failure();

    }

    @PostMapping("permission/update")
    public ResultVo update(@RequestBody Permission permission) {
        int rows = PermissionController.this.permissionService.update(permission);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    @PostMapping("permission/findAll")
    public ResultVo findAll() {
        List<Permission> list = PermissionController.this.permissionService.findAll();
        return ResultVo.success(list);
    }

    @PostMapping("permission/delete")
    public ResultVo delete(@RequestBody String permissionId) {
        System.out.println("permissionId = " + permissionId);
        if (!StringUtils.hasText(permissionId)) {
            throw new RuntimeException("permissionId is null");
        }
        int rows = PermissionController.this.permissionService.delete(Integer.valueOf(permissionId));
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }
}
